package com.qianbao.finance.sync.common.support.orm.db;

import com.alibaba.druid.spring.boot.autoconfigure.DruidDataSourceBuilder;
import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.SqlSessionTemplate;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.transaction.PlatformTransactionManager;

import javax.sql.DataSource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Configuration
@MapperScan(basePackages = {"com.qianbao.finance.sync.modules.elasticsearch.dao"}, sqlSessionFactoryRef = "syncSqlSessionFactory")
public class SyncDataSourceConfig {

    static final String MAPPER_LOCATION =  "classpath:mapper/elasticsearch/*.xml";
    static final String MYBATIS = "classpath:config/mybatis.xml";

    @Bean(name = "syncDataSource")
    @ConfigurationProperties("spring.datasource.druid.syncBizDataSource")
    public DataSource syncDataSource(){
        return DruidDataSourceBuilder.create().build();
    }


    @Bean(name = "syncBizs1DataSource")
    @ConfigurationProperties("spring.datasource.druid.syncBizs1DataSource")
    public DataSource syncBizs1DataSource(){
        return DruidDataSourceBuilder.create().build();
    }
    @Bean(name = "syncBiz0DataSource")
    @ConfigurationProperties("spring.datasource.druid.syncBiz0DataSource")
    public DataSource syncBiz0DataSource(){
        return DruidDataSourceBuilder.create().build();
    }



    @Bean(name = "dynamicDataSource")
    public DataSource dataSource() {
        SyncDynamicDataSource dynamicDataSource = new SyncDynamicDataSource();
        // 默认数据源
        dynamicDataSource.setDefaultTargetDataSource(syncDataSource());

        // 配置多数据源
        Map<Object, Object> dsMap = new HashMap(2);
        dsMap.put("syncDataSource", syncDataSource());
        dsMap.put("syncBizs1DataSource", syncBizs1DataSource());
        dsMap.put("syncBiz0DataSource", syncBiz0DataSource());

        dynamicDataSource.setTargetDataSources(dsMap);
        return dynamicDataSource;
    }
    @Bean
    public PlatformTransactionManager txManager() {
        return new DataSourceTransactionManager(dataSource());
    }
    @Bean
    @ConfigurationProperties(prefix = "mybatis")
    public SqlSessionFactoryBean sqlSessionFactoryBean() throws Exception {
        SqlSessionFactoryBean sqlSessionFactoryBean = new SqlSessionFactoryBean();
        sqlSessionFactoryBean.setDataSource(dataSource());
        sqlSessionFactoryBean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources(MAPPER_LOCATION));
        sqlSessionFactoryBean.setConfigLocation(new PathMatchingResourcePatternResolver().getResource(MYBATIS));
        return sqlSessionFactoryBean;
    }


    @Bean(name = "syncSqlSessionFactory")
        public SqlSessionFactory syncSqlSessionFactory() throws Exception {
            return sqlSessionFactoryBean().getObject();
        }

}
